<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Admin as AdminModel;
use think\Request;

class Admin extends Controller{
    //显示成员列表
    public function adminList(){
        if(empty(session('username')))
        {
            $this->error('请先登录','login/login');
        }
         $admins =Db::table('admin')->order('adminID','asc')->paginate(12);
         $page=$admins->render();
         $this->assign('page',$page);
         $this->assign('admins',$admins);
        
         return $this->fetch();
    }

    //显示添加成员界面
    public function adminAdd(){
        return $this->fetch();
    }
    
    //添加成员
    public function addAdmin(Request $request){
        $admin = new AdminModel();
        $admin->username=$request->param('username');
        $admin->password=md5($request->param('password'));
        $admin->authority=$request->param('authority');
        $admin->save();
        $this->success('添加成功!','shop/shopReload');
    }
    
    //显示编辑成员界面
    public function adminEdit(){
        $admin = AdminModel::get(input('get.username'));
        $this->assign('admin',$admin);
        return $this->fetch();
    }
    
    //编辑成员
    public function editAdmin(Request $request){
        $admin = AdminModel::get(input('post.usernameSave'));
        // dump(input('post.'));
        $admin->password=md5($request->param('password'));
        $admin->authority=$request->param('authority');
        $admin->save();
        $this->success('修改成功!','shop/shopReload');
    }
      
    //删除成员
    public function deleteAdmin(){
        $admin=AdminModel::get(input('post.username'));
        $admin->delete();
        $this->redirect('admin/adminList');
    }
    
    //批量删除
    public function DeleteMore(){
        $admins=input('post.username/s');
        foreach($admins as $admin){
            Db::execute("delete from admin where username=" . $admin);
        }
        //$this->redirect('admin/adminList');
    }
}